package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    /**
     * 根据课程id查询教师信息
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacherDto> list(Long courseId) {
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(new LambdaQueryWrapper<CourseTeacher>()
                .eq(CourseTeacher::getCourseId, courseId));
        //将po转为dto
        List<CourseTeacherDto> list = new ArrayList<>();
        courseTeachers.stream().forEach(item -> {
            CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
            BeanUtils.copyProperties(item, courseTeacherDto);
            list.add(courseTeacherDto);
        });
        return list;
    }

    @Override
    public CourseTeacherDto updateInfo(CourseTeacherDto courseTeacherDto) {
        //根据id查询
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(courseTeacherDto.getId());
        if (courseTeacher != null) {
            BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
            courseTeacherMapper.updateById(courseTeacher);
            return courseTeacherDto;
        }
        return null;
    }

    @Override
    public void delete(Long courseId, Long id) {
        courseTeacherMapper.delete(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId)
                .eq(CourseTeacher::getId, id));
    }

    /**
     * 新增教师信息
     * @param courseTeacherDto
     * @return
     */
    @Override
    public CourseTeacherDto add(CourseTeacherDto courseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
        courseTeacherMapper.insert(courseTeacher);
        //属性拷贝
        BeanUtils.copyProperties(courseTeacher, courseTeacherDto);
        return courseTeacherDto;
    }
}
